<script setup lang="ts">
import { getConsultOrderPayUrl } from '@/services/consult'
import { showLoadingToast, showToast } from 'vant'
import { ref } from 'vue'
const props = withDefaults(
  defineProps<{
    show: boolean
    orderId: string
    actualPayment: number | undefined
    beforeClose?: () => void
    callback: string
  }>(),
  {
    callback: '/room'
  }
)
const payMethod = ref<0 | 1>()
const pay = async () => {
  if (!payMethod.value) return showToast('请选择支付方式')
  // 1. 开启loading
  showLoadingToast({
    message: '正在支付...',
    forbidClick: true,
    duration: 0
  })
  // 2. 发送请求获得 payUrl
  const res = await getConsultOrderPayUrl({
    paymentMethod: payMethod.value,
    orderId: props.orderId,
    payCallback: import.meta.env.VITE_APP_CALLBACK + props.callback
  })
  // 跳转到三方支付平台
  location.href = res.data.payUrl
}
const emits = defineEmits<{
  (e: 'update:show', v: boolean): void
}>()
</script>
<template>
  <!-- 支付抽屉 动作面板  :before-close="beforeClose" -->
  <van-action-sheet
    :show="show"
    @update:show="emits('update:show', false)"
    title="选择支付方式"
    :closeable="false"
    :before-close="beforeClose"
  >
    <div class="pay-type">
      <p class="amount">￥{{ actualPayment }}.00</p>
      <van-cell-group>
        <van-cell title="微信支付" @click="payMethod = 0">
          <template #icon><cp-icon name="consult-wechat" /></template>
          <template #extra>
            <van-checkbox :model-value="payMethod === 0" />
          </template>
        </van-cell>
        <van-cell title="支付宝支付" @click="payMethod = 1">
          <template #icon><cp-icon name="consult-alipay" /></template>
          <template #extra>
            <van-checkbox :model-value="payMethod === 1" />
          </template>
        </van-cell>
      </van-cell-group>
      <div class="btn">
        <van-button type="primary" round block @click="pay"
          >立即支付</van-button
        >
      </div>
    </div>
  </van-action-sheet>
</template>

<style scoped lang="scss">
::v-deep() {
  .van-submit-bar__button {
    font-weight: normal;
    width: 160px;
  }
}
.pay-type {
  .amount {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
  }
  .btn {
    padding: 15px;
  }
  .van-cell {
    align-items: center;
    .cp-icon {
      margin-right: 10px;
      font-size: 18px;
    }
    .van-checkbox :deep(.van-checkbox__icon) {
      font-size: 16px;
    }
  }
}
</style>
